package com.acwing.partition12;

import java.io.*;
import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author `RKC`
 * @date 2022/1/25 11:13
 */
public class AC1174受欢迎的牛 {

    private static final int N = 10010, M = 5 * N;
    private static int[] h = new int[N], e = new int[M], ne = new int[M];

    private static int n = 0, m = 0, idx = 0, timestamp = 0, sccCnt = 0;
    private static boolean[] inStack = new boolean[N];
    private static Deque<Integer> stack = new ArrayDeque<>(N);
    private static int[] dfn = new int[N], low = new int[N], scc = new int[N], size = new int[N], outDegree = new int[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split(" ");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        for (int i = 0; i <= n; i++) h[i] = -1;
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split(" ");
            int a = Integer.parseInt(s[0]), b = Integer.parseInt(s[1]);
            add(a, b);
        }
        for (int i = 1; i <= n; i++) {
            if (dfn[i] == 0) tarjan(i);
        }
        //缩点
        for (int u = 1; u <= n; u++) {
            for (int j = h[u]; j != -1; j = ne[j]) {
                int v = e[j];
                //查看u和v是否是一个连通分量的点，如果不是，则u出度加1
                if (scc[u] != scc[v]) outDegree[scc[u]]++;
            }
        }
        int zero = 0, sum = 0;
        for (int i = 1; i <= sccCnt; i++) {
            if (outDegree[i] == 0) {
                zero++;
                sum += size[i];
                if (zero > 1) {
                    //如果这个DAG有多个出度为0的点，说明没有符合题意的答案
                    sum = 0;
                    break;
                }
            }
        }
        writer.write(sum + "\n");
        writer.flush();
    }

    private static void tarjan(int u) {
        dfn[u] = low[u] = ++timestamp;
        stack.addLast(u);
        inStack[u] = true;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            if (dfn[v] == 0) {
                tarjan(v);
                low[u] = Math.min(low[u], low[v]);
            } else if (inStack[v]) {
                low[u] = Math.min(low[u], dfn[v]);
            }
        }
        if (dfn[u] == low[u]) {
            sccCnt++;
            int v = -1;
            do {
                v = stack.pollLast();
                inStack[v] = false;
                //记录栈中的点都是一个连通分量的点
                scc[v] = sccCnt;
                size[sccCnt]++;
            } while (v != u);
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}
